﻿using System;
using System.Collections.Generic;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Revit;

namespace YJKArch.GlobleSetModule
{
	[Regeneration(RegenerationOption.Manual)]
	[Transaction(TransactionMode.Manual)]
	public class CmdLevelSet2 : YJKCommandAuto
	{
		protected override Result YJKExcute(ExternalCommandData cmdData, ref string message, ElementSet elements)
		{
			this.m_cmdData = cmdData;
			this.UnitType = RevitVersionFuncs.GetDisplayUnitType(this.m_doc, 0);
			try
			{
				this.systemLevelsDatum = new List<LevelsDataSource>();
				foreach (Element element in new FilteredElementCollector(this.m_doc).OfClass(typeof(Level)).ToElements())
				{
					Level level = element as Level;
					LevelsDataSource levelsDataSource = new LevelsDataSource();
					levelsDataSource.LevelIDValue = level.Id.IntegerValue;
					levelsDataSource.Name = level.Name;
					Parameter parameter = level.get_Parameter(BuiltInParameter.LEVEL_ELEV);
					double elevation = double.Parse(Unit.CovertFromAPI(this.UnitType, parameter.AsDouble()).ToString("#.0"));
					levelsDataSource.Elevation = elevation;
					this.systemLevelsDatum.Add(levelsDataSource);
				}
				LevelsDataComparer comparer = new LevelsDataComparer();
				this.systemLevelsDatum.Sort(comparer);
				using (LevelsForm levelsForm = new LevelsForm(this))
				{
					levelsForm.ShowDialog();
				}
			}
			catch (Exception ex)
			{
				message = ex.Message;
				return Autodesk.Revit.UI.Result.Failed;
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		public List<LevelsDataSource> SystemLevelsDatum
		{
			get
			{
				return this.systemLevelsDatum;
			}
			set
			{
				this.systemLevelsDatum = value;
			}
		}

		public bool SetLevel(int levelIDValue, string levelName, double levelElevation)
		{
			bool result;
			try
			{
				ElementId id = new ElementId(levelIDValue);
				Level level = RevitVersionFuncs.GetElement(this.m_doc, id) as Level;
				level.get_Parameter(BuiltInParameter.LEVEL_ELEV).SetValueString(levelElevation.ToString());
				level.Name = levelName;
				result = true;
			}
			catch (Exception)
			{
				result = false;
			}
			return result;
		}

		public void CreateLevel(string levelName, double levelElevation)
		{
			Level level = this.m_doc.HyNewLevel(levelElevation);
			level.get_Parameter(BuiltInParameter.LEVEL_ELEV).SetValueString(levelElevation.ToString());
			level.Name = levelName;
			ViewPlan viewPlan = RevitVersionFuncs.NewViewPlan(this.m_doc, levelName, level, (Autodesk.Revit.DB.ViewPlanType)1);
			if (viewPlan != null)
			{
				viewPlan.get_Parameter(BuiltInParameter.VIEW_DISCIPLINE).Set(1);
				Parameter parameter = viewPlan.GetParameter("视图分类-父");
				if (parameter != null)
				{
					string text = "";
					if (this.GetPlanViewDirParent(ref text))
					{
						parameter.Set(text);
					}
				}
			}
		}

		private bool GetPlanViewDirParent(ref string strParent)
		{
			foreach (ElementId elementId in new FilteredElementCollector(this.m_doc).OfCategory(BuiltInCategory.OST_Views).OfClass(typeof(ViewPlan)).ToElementIds())
			{
				ViewPlan viewPlan = this.m_doc.GetElement(elementId) as Autodesk.Revit.DB.ViewPlan;
				if (viewPlan != null && (int)viewPlan.ViewType == 1)
				{
					Parameter parameter = viewPlan.GetParameter("视图分类-父");
					if (parameter != null)
					{
						strParent = parameter.AsString();
						return true;
					}
				}
			}
			return false;
		}

		public void DeleteLevel(int IDValueOfLevel)
		{
			ElementId elementId = new ElementId(IDValueOfLevel);
			this.m_doc.Delete(elementId);
		}

		public void ResetLevelType(ElementId firstId, ElementId standardId)
		{
			LevelType levelType = RevitVersionFuncs.GetElement(this.m_doc, firstId) as LevelType;
			LevelType levelType2 = RevitVersionFuncs.GetElement(this.m_doc, standardId) as LevelType;
			IEnumerable<Element> enumerable = ElementFiltering.FindSpecialElements(this.m_doc, typeof(Level), BuiltInCategory.OST_Levels);
			int num = 0;
			foreach (Element element in enumerable)
			{
				Level level = element as Level;
				if (num == 0)
				{
					level.ChangeLevelType(levelType);
				}
				else
				{
					level.ChangeLevelType(levelType2);
				}
				num++;
			}
		}

		public DisplayUnitType UnitType;

		private List<LevelsDataSource> systemLevelsDatum;
	}
}
